package com.hdjd.controller;

import com.hdjd.entities.OrderDetail;
import com.hdjd.service.BusinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 刘子强
 * 2021/1/23 17:32
 */
@Controller
@RequestMapping("/business")
public class BusinessController {
    @Autowired
    private BusinessService businessService;

    //商家注册
    @ResponseBody
    @RequestMapping(value = "/registerBusiness", method = RequestMethod.PUT)
    public Map<String, Object> registerBusiness(Integer accId, String accPwd, String accTelephone, String shopName){
        Map<String, Object> map = new HashMap<>();

        boolean flag = false;
        try {
            flag = this.businessService.bussinessRegister(accId, accPwd, accTelephone, shopName);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("msg", "申请失败");
            return map;
        }

        if(flag){
            map.put("code", 1001);
            map.put("msg", "申请成功");
        }else{
            map.put("code", 1002);
            map.put("msg", "申请失败");
        }
        return map;
    }
    //查询商家未完成订单
    @ResponseBody
    @RequestMapping(value = "/selectUnFinishOrder", method = RequestMethod.GET)
    public Map<String, Object> selectUnFinishOrder(Integer accId, Integer ordStatus, @RequestParam("page") Integer pageCurrent, @RequestParam("limit") Integer pageSize){
        Map<String, Object> map = new HashMap<>();

        List<OrderDetail> orderDetails;
        Integer count;
        try {
             orderDetails = this.businessService.selectUnFinishOrder(accId, ordStatus, pageCurrent, pageSize);
             count = this.businessService.countUnFinishOrder(accId, ordStatus);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("msg", "查询失败");
            return map;
        }
        if(!orderDetails.isEmpty()){
            map.put("code", 0);        //layui 查询成功必须返回 code = 0
            map.put("msg", "查询成功");
            map.put("data", orderDetails);  //layui 查询必须返回的数据名 必须叫 data
            map.put("count", count);
            return map;
        }else{
            map.put("code", 1003);
            map.put("msg", "查询无果");
            return map;
        }
    }
    //商家发货
    @ResponseBody
    @RequestMapping(value = "/deliverGoods", method = RequestMethod.PUT)
    public Map<String, Object> deliverGoods(Integer detId){
        Map<String, Object> map = new HashMap<>();

       boolean flag = false;
        try {
            flag = this.businessService.deliverGoods(detId);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("msg", "发货失败");
            return map;
        }
        if(flag){
            map.put("code", 1001);
            map.put("msg", "发货成功");
            return map;
        }else{
            map.put("code", 1002);
            map.put("msg", "发货失败");
            return map;
        }
    }
    //商家退款
    @ResponseBody
    @RequestMapping(value = "/refund", method = RequestMethod.PUT)
    public Map<String, Object> refund(Integer detId, Integer accId, Double totalPrice){
        Map<String, Object> map = new HashMap<>();

       boolean flag = false;
        try {
            flag = this.businessService.refund(detId, accId, totalPrice);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("msg", "退款失败");
            return map;
        }
        if(flag){
            map.put("code", 1001);
            map.put("msg", "退款成功");
            return map;
        }else{
            map.put("code", 1002);
            map.put("msg", "退款失败");
            return map;
        }
    }

    //查询商家所有已完成订单及数据笔数
    @ResponseBody
    @RequestMapping(value = "/selectFinishOrder", method = RequestMethod.GET)
    public Map<String, Object> selectFinishOrder(Integer accId, Integer ordStatus, @RequestParam("page") Integer pageCurrent, @RequestParam("limit") Integer pageSize, String startDate){
        Map<String, Object> map = new HashMap<>();

        List<OrderDetail> orderDetails;
        Integer count;
        try {
            orderDetails = this.businessService.selectFinishOrder(accId, ordStatus, pageCurrent, pageSize, startDate);
            count = this.businessService.countFinishOrder(accId, ordStatus,startDate);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1);
            map.put("msg", "查询失败");
            return map;
        }
        if(!orderDetails.isEmpty()){
            map.put("code", 0);
            map.put("msg", "查询成功");
            map.put("data", orderDetails);
            map.put("count", count);
            return map;
        }else{
            map.put("code", 2);
            map.put("msg", "查询无果");
            return map;
        }
    }

}
